/*******************************************************************************
  * @file                   TIMER.c
  * @Author:                MQjehovah                 mail:MQjehovah@hotmail.com
  * @version                1.0.0
  * @date                   2017.5.9
  * @brief                  
  ******************************************************************************
  * @attention
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "TIMER.h"

/* Definition ----------------------------------------------------------------*/

/* Functions -----------------------------------------------------------------*/
/*******************************************************************************
  * @brief  TIM4配置              
  * @param  freq：设置定时器频率 
			频率计算公式：f=TIMCLK/((Period+1)*(Prescaler+1))
  * @retval None              
  * @Note   默认APB1时钟为36M              
*******************************************************************************/
void TIM_init(TIM_TypeDef * TIMx, u16 freq)
{  
	//初始化结构体
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	//freq=72M/(Prescaler+1)*Period
	
	//时钟统一由RCC_Config配置
	TIM_DeInit(TIMx);
	TIM_TimeBaseStructure.TIM_Period=freq-1;                 //定时器周期
	TIM_TimeBaseStructure.TIM_Prescaler=7200-1;              //预分频数
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV2;    //TIM2时钟分频,为1表示不分频
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//定时器计数为向上计数模式
	TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);     
//	TIM_ClearFlag(TIMx, TIM_FLAG_Update);                    //清除定时器2的溢出标志位
//	TIM_ITConfig(TIMx,TIM_IT_Update,ENABLE);                 //使能定时器2溢出中断
	PWM_Config(TIMx,0,0,50,50);                              //定时器PWM输出
	TIM_Cmd(TIMx, ENABLE);                                   //定时器2使能
} 

/*******************************************************************************
  * @brief  TIM配置              
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
void TIM_Config(TIM_TypeDef * TIMx)
{  
	//初始化结构体
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
	//时钟统一由RCC_Config配置
	TIM_DeInit(TIMx);
	TIM_TimeBaseStructure.TIM_Period=100-1;                  //定时器周期
	TIM_TimeBaseStructure.TIM_Prescaler=3600-1;              //预分频数
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;    //TIM2时钟分频,为1表示不分频
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//定时器计数为向上计数模式
	TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);     
//	TIM_ClearFlag(TIMx, TIM_FLAG_Update);                    //清除定时器2的溢出标志位
//	TIM_ITConfig(TIMx,TIM_IT_Update,ENABLE);                 //使能定时器2溢出中断
	PWM_Config(TIMx,0,0,0,50);                              //定时器PWM输出
	TIM_Cmd(TIMx, ENABLE);                                   //定时器2使能
} 

/*******************************************************************************
  * @brief  改变频率             
  * @param  None              
  * @retval None              
  * @Note   未实现             
*******************************************************************************/
void Change_F(TIM_TypeDef * TIMx,u16 arr,u16 pre)
{  
	//初始化结构体
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  

	TIM_TimeBaseStructure.TIM_Period=arr;                   //定时器周期
	TIM_TimeBaseStructure.TIM_Prescaler=pre;                //预分频数
	TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);     

	TIM_Cmd(TIMx, ENABLE);                                   //定时器2使能
}


/*******************************************************************************
  * @brief  PWM配置              
  * @param  TIMx 定时器
			CCR1,CCR2,CCR3,CCR4:四个通道的重装值
  * @retval None              
  * @Note   None              
*******************************************************************************/
void PWM_Config(TIM_TypeDef * TIMx, u16 CCR1,u16 CCR2,u16 CCR3,u16 CCR4)
{
	TIM_OCInitTypeDef  TIM_OCInitStructure;  
	GPIO_InitTypeDef GPIO_InitStructure;

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;                           //配置为PWM模式1  
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;                
    TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High;                    //当定时器计数值小于CCR1时为高电平  
	
	/* PWM1 Mode configuration: Channel1 */  
	if(CCR1!=0)
	{
		//PWM输出引脚
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;       //Channel1
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOB,&GPIO_InitStructure);
		
		TIM_OCInitStructure.TIM_Pulse = CCR1;                                        //设置跳变值，当计数器计数到这个值时，电平发生跳变  
		TIM_OC1Init(TIMx, &TIM_OCInitStructure);                                    //使能通道1      
		TIM_OC1PreloadConfig(TIMx, TIM_OCPreload_Enable);  
	}
	
	
	/* PWM1 Mode configuration: Channel2 */  
	if(CCR2!=0)
	{
		//PWM输出引脚
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;       //Channel2
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOB,&GPIO_InitStructure);
		
		TIM_OCInitStructure.TIM_Pulse = CCR2;                                       //设置通道2的电平跳变值，输出另外一个占空比的PWM  
		TIM_OC2Init(TIMx, &TIM_OCInitStructure);                                    //使能通道2  
		TIM_OC2PreloadConfig(TIMx, TIM_OCPreload_Enable);  
	}
	
	/* PWM1 Mode configuration: Channel3 */  
	if(CCR3!=0)
	{
		//PWM输出引脚
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;       //Channel3
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOB,&GPIO_InitStructure);
		
		TIM_OCInitStructure.TIM_Pulse = CCR3;                                       //设置通道2的电平跳变值，输出另外一个占空比的PWM  
		TIM_OC3Init(TIMx, &TIM_OCInitStructure);                                    //使能通道2  
		TIM_OC3PreloadConfig(TIMx, TIM_OCPreload_Enable);  
	}
	
	/* PWM1 Mode configuration: Channel4 */  
	if(CCR4!=0)
	{
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;       //Channel4
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOB,&GPIO_InitStructure);
		
		TIM_OCInitStructure.TIM_Pulse = CCR4;                                       //设置通道2的电平跳变值，输出另外一个占空比的PWM  
		TIM_OC4Init(TIMx, &TIM_OCInitStructure);                                    //使能通道2  
		TIM_OC4PreloadConfig(TIMx, TIM_OCPreload_Enable);  
	}
	
	TIM_CtrlPWMOutputs(TIMx,ENABLE);                                            //MOE 主输出使能        
    TIM_ARRPreloadConfig(TIMx, ENABLE);                                         //使能TIM3重载寄存器ARR  
}
/*********************************END OF FILE**********************************/
